
function getType(){
    let typeSelect = $("#type");
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getAllType",
    }).done(function (data){
        for (const key in data) {
            let type = data[key];
            let option = $("<option value=" + type.id + ">" + type.name + "</option>");
            typeSelect.append(option);
        }
    });
}

function getChapter() {
    let chapterSelect = $("#chapter");
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getAllChapter",
    }).done(function (data){
        for (const key in data) {
            let chapter = data[key];
            let option = $("<option value=" + chapter.id + ">" + chapter.name + "</option>");
            chapterSelect.append(option);
        }
    });
}

function submitExercise(){
    let paramIsOk;

    let eid = $("#eid").val();

    let title = $("#title").val().trim();
    let answer = $("#answer").val().trim();
    let type = $("#type").val();
    let chapter = $("#chapter").val();
    let optionA = $("#optionA").val().trim();
    let optionB = $("#optionB").val().trim();
    let optionC = $("#optionC").val().trim();
    let optionD = $("#optionD").val().trim();

    if (title === null || title === ""){
        paramIsOk = false;
        alert("请输入题目！");
    } else if (answer === null || answer === ""){
        paramIsOk = false;
        alert("请选择答案！");
    } else if (type === null || type === ""){
        paramIsOk = false;
        alert("请选择题目类型");
    } else if (chapter === null || chapter === ""){
        paramIsOk = false;
        alert("请选择所属章节");
    } else if (optionA === null || optionA === ""){
        paramIsOk = false;
        alert("请输入答案A");
    } else if (optionB === null || optionB === ""){
        paramIsOk = false;
        alert("请输入答案B");
    } else if (optionC === null || optionC === ""){
        paramIsOk = false;
        alert("请输入答案C");
    } else if (optionD === null || optionD === ""){
        paramIsOk = false;
        alert("请输入答案D");
    } else {
        paramIsOk = true;
    }

    if (paramIsOk){
        $.ajax({
            type: "POST",
            url: "webapi/exercise/saveExercise",
            data: {
                eid: eid,
                title: title,
                answer: answer,
                type: type,
                chapter: chapter,
                optionA: optionA,
                optionB: optionB,
                optionC: optionC,
                optionD: optionD
            }
        }).done(function (data){
            if (data === 1){
                alert("操作成功");
                window.location.href = "/toManageExercise";
            } else if (data === -1){
                alert("操作失败")
            }
        });
    }
}

function addChapter() {
    let chapterName = $("#chapterName").val();
    $.ajax({
        type: "POST",
        url: "webapi/exercise/addChapter",
        data: {
            chapterName: chapterName
        }
    }).done(function (data){
       if (data === 1){
           alert("添加成功");
           window.location.href = "/toAddExercise";
       } else if (data === 0){
           alert("该章节已存在");
       } else if (data === -1){
           alert("添加失败");
       }
    });
}

/**
 * 获取用户选择的文件名
 */
function getSelectFilename(){
    let file = $("#file").val();
    $("#uploadFilename").val(file);
}

function saveExerciseByFile(){
    let files = $("#file").prop("files");
    let data = new FormData;
    data.append("file", files[0]);
    $.ajax({
        type: "POST",
        url: "/webapi/exercise/saveExerciseByFile",
        data: data,
        processData: false, //无须序列化
        contentType: false, //无须处理
        cache: false //无须缓存
    }).done(function (data){
        if (data > 0){
            alert("添加成功，新增" + data + "条数据");
            loadFileTable();
        } else if (data === -1){
            alert("添加失败");
        }
    });
}